package com.lhf.aicloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * @author: lhf
 * @date: Created in 2024/08/12
 * @description: spring 线程池配置
 */
@Configuration
public class ThreadPoolConfig {
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        // 核心线程数，cpu 个数 + 1
        threadPoolTaskExecutor.setCorePoolSize(Runtime.getRuntime().availableProcessors() + 1);
        // 最大线程数，cpu 个数 * 2 + 1
        threadPoolTaskExecutor.setMaxPoolSize(Runtime.getRuntime().availableProcessors() * 2 + 1);
        // 队列容量
        threadPoolTaskExecutor.setQueueCapacity(100000);
        // 拒绝策略
        threadPoolTaskExecutor.setRejectedExecutionHandler((r, executor) -> {
            // 1. todo 保存拒绝任务

            // 2. todo 通知相关负责人手动处理
        });

        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }
}
